package com.teacher.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.taglibs.standard.tag.common.fmt.FormatDateSupport;

/**
 * 时间操作工具类
 * @author XHFSJ
 *
 */
public class DateUtils {

	/**
	 * 将日期的某一个分量加上指定数值
	 * @param src
	 * 		原始日期
	 * @param nums
	 * 		加上的数值
	 * @param field
	 * 		分量
	 * @return
	 */
	public static Date dateAdd(Date src,int nums,int field){
		Date result = new Date();
		result.setYear(src.getYear());
		result.setMonth(src.getMonth());
		result.setDate(src.getDate());
		result.setHours(src.getHours());
		result.setMinutes(src.getMinutes());
		result.setSeconds(src.getSeconds());
		switch (field) {
		case Calendar.YEAR:
			result.setYear(src.getYear()+nums);
			break;
		case Calendar.MONTH:
			result.setMonth(src.getMonth()+nums);
			break;
		case Calendar.DAY_OF_MONTH:
			result.setDate(src.getDate()+nums);
			break;
		case Calendar.HOUR_OF_DAY:
			result.setHours(src.getHours()+nums);
			break;
		case Calendar.MINUTE:
			result.setMinutes(src.getMinutes() + nums);
			break;
		case Calendar.SECOND:
			result.setSeconds(src.getSeconds() + nums);
			break;
		default:
			break;
		}
		return result;
	}
	
	
	/**
	 * 格式化日期
	 * @param src
	 * @param format
	 * @return
	 */
	public static String formatDate(Date src,String format){
		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		result = sdf.format(src);
		return result;
	}
	
	/**
	 * 将日期转换为整数
	 * @param src
	 * @return
	 */
	@SuppressWarnings("deprecation")
	private static int DateToInteger(Date src){
		int result = 0;
		result = src.getDate()*src.getYear()+src.getMonth();
		int hour = src.getHours();
		int minute = src.getMinutes();
		int second = src.getSeconds();
		result *= hour!=0?hour:1;
		result *= minute!=0?minute:1;
		result *= second!=0?second:1;
		return result;
	}
	
	/**
	 * 计算两个日期相差天数
	 * @param d1
	 * @param d2
	 * @return
	 * 		如果d1>d2,则返回0，反之返回相差天数
	 */
	public static long between_Days(Date d1,Date d2){
		long result = 0;
		Calendar c = Calendar.getInstance();
		c.setTime(d1);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(d2);
		if(c.after(c1)){
			return 0;
		}
		result = Math.abs(c.getTimeInMillis()-c1.getTimeInMillis());
		result = result/(1000*60*60*24) + (result%(1000*3600*24)>0?1:0);
		
		return result;
	}
	
	/**
	 * 检查当前日期是否在指定的日期区间内
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean CompareDate(Date d1,Date d2){
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		Calendar curDate = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		return curDate.after(c1)&&curDate.before(c2);
	}
	
	public static void main(String[] args){
//		Date d = new Date();
//		Date d2 = dateAdd(d, -1, Calendar.DAY_OF_MONTH);
//		d2 = dateAdd(d2, -1, Calendar.HOUR_OF_DAY);
//		Date d3 = dateAdd(d, 2, Calendar.DAY_OF_MONTH);
//		System.out.println(d2);
//		System.out.println(d3);
//		System.out.println(between_Days(d3, d2));
		String s = "F:\\test\\r.txt";
		String substr = "test\\";
		System.out.println(s.substring(s.indexOf(substr)+substr.length()));
	}
}
